<?php
/**
 * User zgh
 * Email 1803897171@qq.com
 * Date 2021/7/29
 **/

namespace app\common\util;

use FFMpeg\Coordinate\TimeCode;
use FFMpeg\FFMpeg;

/**
 * 视频工具类
 * @安装方法  https://hacpai.com/article/1536833644371
 */
class FFMpegUtil
{
    protected $ffmpeg = null;


    private function __construct()
    {
        if (empty($this->ffmpeg)) {
            $system_type = strtolower(php_uname('s'));
            if (strrpos($system_type, "windows") !== false) {
                $this->ffmpeg = FFMpeg::create([
                    'ffmpeg.binaries' => 'C:/BtSoft/ffmpeg/bin/ffmpeg.exe',
                    'ffprobe.binaries' => 'C:/BtSoft/ffmpeg/bin/ffprobe.exe',
                    'timeout' => 3600,
                    'ffmpeg.threads' => 12,
                ]);
            } else {
                $this->ffmpeg = FFMpeg::create([
                    'ffmpeg.binaries' => '/usr/bin/ffmpeg',
                    'ffprobe.binaries' => '/usr/bin/ffprobe',
                    'timeout' => 3600,
                    'ffmpeg.threads' => 12,
                ]);
            }
        }
    }

    public static function Instance()
    {
        return new static();
    }

    /**
     * 获取视频截图
     * @param $source
     * @param $target
     * @param int $second
     * @return \FFMpeg\Media\Frame
     */
    public function getScreenshot($source, $target, $second = 1)
    {
        $video = $this->ffmpeg->open($source);
        $frame = $video->frame(TimeCode::fromSeconds(intval($second)));
        return $frame->save($target);
    }

    /**
     * 获取视频的信息
     *array(11) {
     * ["filename"] => string(106) "/www/wwwroot/xy.shop.com/application//../public/trim1623313234474.85CBC57F-C6A5-4E71-9D79-F54D26C4E1AA.mp4"
     * ["nb_streams"] => int(2)
     * ["nb_programs"] => int(0)
     * ["format_name"] => string(23) "mov,mp4,m4a,3gp,3g2,mj2"
     * ["format_long_name"] => string(15) "QuickTime / MOV"
     * ["start_time"] => string(9) "-0.023220"
     * ["duration"] => string(10) "146.867000"
     * ["size"] => string(8) "13861249"
     * ["bit_rate"] => string(6) "755036"
     * ["probe_score"] => int(100)
     * ["tags"] => array(4) {
     * ["major_brand"] => string(4) "isom"
     * ["minor_version"] => string(3) "512"
     * ["compatible_brands"] => string(16) "isomiso2avc1mp41"
     * ["encoder"] => string(13) "Lavf58.29.100"
     * }
     * }
     * @param $source
     * @param string $key
     */
    public function getInfo($source, $key = "")
    {
        $video = $this->ffmpeg->open($source);
        $format = $video->getFormat();
        $info = $format->all();
        if (empty($key)) {
            return $info;
        } else {
            return $info[$key];
        }
    }

    /**
     * 秒数转变成小时
     * @param $seconds
     * @return string
     */
    public static function secondToHour($seconds)
    {
        if ($seconds > 3600) {
            $hours = intval($seconds / 3600);
            $time = $hours . ":" . gmstrftime('%M:%S', $seconds);
        } else {
            $time = gmstrftime('%H:%M:%S', $seconds);
        }
        return $time;
    }
}